//
//  AppDelegate.m
//  MagicBox
//
//  Created by dllo on 16/8/9.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "AppDelegate.h"
#import "HomePage_ViewController.h"
#import "Find_ViewController.h"
#import "News_ViewController.h"
#import "EMSDK.h"  // 初始化SDK

#import "directSeedingViewController.h"
#import "mySelfViewController.h"

@interface AppDelegate ()<EMContactManagerDelegate, EMChatManagerDelegate>

@end

@implementation AppDelegate

+ (AppDelegate *)share
{
    return (AppDelegate *)[UIApplication sharedApplication].delegate;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    
    //首页tabbar
    HomePage_ViewController *HomePageVC = [[HomePage_ViewController alloc]init];
    UINavigationController *HomePageNa = [[UINavigationController alloc]initWithRootViewController:HomePageVC];
    
    UIImage *HomePageImage = [UIImage imageNamed:@"HomePageRed.png"];
    HomePageImage = [HomePageImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    HomePageNa.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"首页" image:[UIImage imageNamed:@"HomePage.png"] selectedImage:HomePageImage];
   
    
    
    directSeedingViewController *directSeedingVC = [[directSeedingViewController alloc]init];
    UINavigationController *directSeedingNa = [[UINavigationController alloc]initWithRootViewController:directSeedingVC];
    
    UIImage *directSeedingImage = [UIImage imageNamed:@"zhiBoRed.png"];
    directSeedingImage = [directSeedingImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    directSeedingNa.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"直播" image:[UIImage imageNamed:@"zhiBo.png"] selectedImage:directSeedingImage];
    
    
    

    
    Find_ViewController *FindVC = [[Find_ViewController alloc]init];
    UINavigationController *FindNa = [[UINavigationController alloc]initWithRootViewController:FindVC];
    UIImage *FindImage = [UIImage imageNamed:@"FindRed.png"];
    FindImage = [FindImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    FindNa.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"发现" image:[UIImage imageNamed:@"Find.png"] selectedImage:FindImage];
    
    
    
    
    News_ViewController *NewsVC = [[News_ViewController alloc]init];
    UINavigationController *NewsNa = [[UINavigationController alloc]initWithRootViewController:NewsVC];
    UIImage *NewsImage = [UIImage imageNamed:@"NewsRed.png"];
    NewsImage = [NewsImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
   NewsNa.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"发布" image:[UIImage imageNamed:@"News.png"] selectedImage:NewsImage];
  
    mySelfViewController *mySelfVC = [[mySelfViewController alloc]init];
    UINavigationController *mySelfNa = [[UINavigationController alloc]initWithRootViewController:mySelfVC];
    UIImage *mySelfImage = [UIImage imageNamed:@"mySelfRed.png"];
  mySelfImage = [mySelfImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    mySelfNa.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"发布" image:[UIImage imageNamed:@"mySelf.png"] selectedImage:mySelfImage];

    
    
    
    
    
    UITabBarController *Tabar = [[UITabBarController alloc] init];
    Tabar.viewControllers = @[HomePageNa,directSeedingNa,FindNa,NewsNa,mySelfNa];
    
    self.window.rootViewController = Tabar;
    
    
//     推送会用到的, 前提是需要提前注册推送证书
    // 初始化SDK
    //AppKey:注册的AppKey
    //apnsCertName:推送证书名（不需要加后缀）
    EMOptions *options = [EMOptions optionsWithAppkey:@"jpcc282#magicbox"];
    options.apnsCertName = @"";
    [[EMClient sharedClient] initializeSDKWithOptions:options];
   
    
//    那我就在这判断是否是自动登录, 如果是, 就在这签代理
     [[EMClient sharedClient].contactManager addDelegate:self  delegateQueue:dispatch_get_main_queue()];
    
    
    [[EMClient sharedClient].chatManager addDelegate:self delegateQueue:nil];
    
     return YES;
}


-(void)didReceiveMessages:(NSArray *)aMessages{
    NSLog(@"________%@", aMessages);
    
    for (EMMessage *message in aMessages) {
        EMMessageBody *msgBody = message.body;
        
        
        switch (msgBody.type)
        {
            case EMMessageBodyTypeText:
            {
                
            }
                break;
            case EMMessageBodyTypeImage:
            {
                EMImageMessageBody *body = ((EMImageMessageBody *)msgBody);
                NSLog(@"大图remote路径 -- %@"   ,body.remotePath);
                NSLog(@"大图local路径 -- %@"    ,body.localPath); // // 需要使用sdk提供的下载方法后才会存在
                NSLog(@"大图的secret -- %@"    ,body.secretKey);
                NSLog(@"大图的W -- %f ,大图的H -- %f",body.size.width,body.size.height);
                NSLog(@"大图的下载状态 -- %u",body.downloadStatus);
            }
                break;
            default:
                break;
        }
        
        NSDictionary *ext = message.ext;
        NSLog(@"消息中的扩展属性是 -- %@",ext);
        
        
    }
    
    
    
}


-(void)didReceiveCmdMessages:(NSArray *)aCmdMessages{
    NSLog(@"@@@@@@@@@@@@@@%@", aCmdMessages);
    for (EMMessage *message in aCmdMessages) {
        EMCmdMessageBody *body = (EMCmdMessageBody *)message.body;
        NSLog(@"收到的action是 -- %@",body.action);
        
        
        // cmd消息中的扩展属性
        NSDictionary *ext = message.ext;
        NSLog(@"cmd消息中的扩展属性是 -- %@",ext);

    }

}





#pragma mark -环信回调
-(void)didReceiveFriendInvitationFromUsername:(NSString *)aUsername message:(NSString *)aMessage{
    
    [[CCContactTool shareContactTool] addMessages:aMessage userName:aUsername];

    
}

-(void)didReceiveAddedFromUsername:(NSString *)aUsername{
    
    NSLog(@"我收到了请求YEYEYEYEY我是AppDelegate: %@", aUsername);
    [[CCContactTool shareContactTool].ReceiveAddedFromUserNameArray addObject:aUsername];
    
    
}






- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}


// App进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    
    [[EMClient sharedClient] applicationDidEnterBackground:application];
}

//App 将要从后台返回
- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    
    [[EMClient sharedClient] applicationWillEnterForeground:application];
}

// 程序退出或者终止时的操作
- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    //    [EMClient sharedClient] applicationw
}








- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (CMPedometer *)sharedPedometer
{
    static CMPedometer *sharedsharedPedometerrInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedsharedPedometerrInstance = [[CMPedometer alloc] init];
    });
    return sharedsharedPedometerrInstance;
}


-(void)gotoGetStepCountFrom:(NSDate *)fromeDate to:(NSDate *)toDate CallBack:(void(^)(NSString *steps))callBack{   //根据起始期，终止日期进行查询步行数量
    
    
    
    //判断记步功能
    if ([CMPedometer isStepCountingAvailable]) {
        [[[AppDelegate share] sharedPedometer] queryPedometerDataFromDate:fromeDate toDate:toDate withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {
            if (error) {
                NSLog(@"error====%@",error);
            }else {
                //                NSLog(@"AAA步数====%@",pedometerData.numberOfSteps);
                //                NSLog(@"AAA距离====%@",pedometerData.distance);
                NSString *numberOfSteps = [NSString stringWithFormat:@"%@",pedometerData.numberOfSteps];
                callBack(numberOfSteps);
            }
        }];
    }else{
        NSLog(@"记步功能不可用");
    }
    
    
}
@end
